package com.qxmly.fm.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qxmly.fm.DetailActivity;
import com.qxmly.fm.R;
import com.qxmly.fm.adapters.RecommendAdapter;
import com.qxmly.fm.base.BaseFragment;
import com.qxmly.fm.interfaces.IrecommendViewCallBack;
import com.qxmly.fm.presenters.DetailPresenter;
import com.qxmly.fm.presenters.RecommendPresenter;
import com.qxmly.fm.views.UILoader;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

/**
 * Q
 * Date:2021/11/23$
 **/
public class RecommendFragment extends BaseFragment implements IrecommendViewCallBack, UILoader.OnRetryClickListener, RecommendAdapter.OnRecommendItemClick {
    private static final String TAG ="RecommendFragment" ;
    private View rootView;
    private RecyclerView mRecommendRv;
    private RecommendAdapter mRecommendAdapter;
    private RecommendPresenter mRecommendPresenter;
    private UILoader mUILoader;

    @Override
    protected View onSubViewLoaded(final LayoutInflater layoutInflater, ViewGroup container) {

        mUILoader =new UILoader(getContext()) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return CreateSuccessView(layoutInflater,container);
            }
        };
       rootView = layoutInflater.inflate(R.layout.fragment_recommend,container,false);
        //选择控件
        mRecommendPresenter = RecommendPresenter.getInstance();
        //设置接口注册
        mRecommendPresenter.registerViewCallBack(this);
        mRecommendPresenter.getRecommend();
        if(mUILoader.getParent() instanceof ViewGroup){
            ((ViewGroup)mUILoader.getParent()).removeView(mUILoader);
        };
        mUILoader.setOnRetryClickListener(this);
        //返回view
        return mUILoader;

    }

    private View CreateSuccessView( LayoutInflater layoutInflater, ViewGroup container) {
        mRecommendRv = rootView.findViewById(R.id.Recommend);
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecommendRv.setLayoutManager(linearLayoutManager);
        mRecommendRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                //super.getItemOffsets(outRect, view, parent, state);
                //dp转像素,设置间距
                outRect.top= UIUtil.dip2px(view.getContext(),5);
                outRect.bottom=UIUtil.dip2px(view.getContext(),5);
                outRect.left=UIUtil.dip2px(view.getContext(), 5);
                outRect.right=UIUtil.dip2px(view.getContext(), 5);
            }
        });
        //设置适配器
        mRecommendAdapter = new RecommendAdapter();
        mRecommendRv.setAdapter(mRecommendAdapter);
        mRecommendAdapter.setOnRecommendItemClickListener(this);
        mUILoader.setOnRetryClickListener(this);
//        //推荐页面取数据
//        //getRecommendData();
//        //获取逻辑层对象
//        mRecommendPresenter = RecommendPresenter.getInstance();
//        //设置接口注册
//        mRecommendPresenter.registerViewCallBack(this);
//        mRecommendPresenter.getRecommend();
//        if(mUILoder.getParent() instanceof ViewGroup){
//            ((ViewGroup)mUILoder.getParent()).removeView(mUILoder);
//        };
        //返回view
        return rootView;
    }

    //    //获取推荐内容
//    private void getRecommendData(){
//        //封装参数
//        Map<String, String> map = new HashMap<String, String>();
//        //一页数据返回条数
//        map.put(DTransferConstants.LIKE_COUNT, Constants.RECOMMAND_COUNT+"");
//        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
//
//
//            @Override
//            //获取成功
//            public void onSuccess(@Nullable GussLikeAlbumList gussLikeAlbumList) {
//                LogUtil.d(TAG,"thread name ->"+Thread.currentThread().getName());
//                if(gussLikeAlbumList!=null){
//                    List<Album> album_list=gussLikeAlbumList.getAlbumList();
//                   // LogUtil.d(TAG,"size->"+album_list);
//                    upRecommendUI(album_list);
//                }
//            }
//
//            @Override
//            //获取失败
//            public void onError(int i, String s) {
//                LogUtil.d(TAG,"error ->"+i);
//                LogUtil.d(TAG,"errorMsg->"+s);
//
//            }
//        });
//    }
//    private void upRecommendUI(List<Album>album_list){
//        //数据给适配器，更新UI
//        mRecommendAdapter.setDate(album_list);
//    }

    @Override
    public void onRecommendLoad(List<Album> result) {
        //获取推荐内容时，此方法被调用
        //更新UI
        mRecommendAdapter.setDate(result);
        mUILoader.upDateStatus(UILoader.UIStatus.SUCCESS) ;
    }

    @Override
    public void onNetWorkError() {
        mUILoader.upDateStatus(UILoader.UIStatus.NETWORK_ERROR) ;

    }

    @Override
    public void onEmpty() {
        mUILoader.upDateStatus(UILoader.UIStatus.EMPTY) ;
    }

    @Override
    public void onLoading() {
        mUILoader.upDateStatus(UILoader.UIStatus.LOADING) ;
    }



//    @Override
//    public void onLoader(List<Album> result) {
//
//    }
//
//    @Override
//    public void onRefresh(List<Album> result) {
//
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //取消接口注册
        if (mRecommendPresenter!=null){
            mRecommendPresenter.unRegisterViewCallBack(this);
        }
    }

    @Override
    public void onRetryClick() {
        //点击刷新
        if(mRecommendPresenter!=null){
            mRecommendPresenter.getRecommend();
        }
    }

    @Override
    public void onItemClick(int position, Album album) {
        //实现点击跳转
        DetailPresenter.getInstance().setTargetAlbum(album);
        Intent intent = new Intent(getContext(), DetailActivity.class);
        startActivity(intent);

    }
}
